package com.warehouse_management.system.controller;

import com.alibaba.fastjson.JSON;
import com.warehouse_management.login.model.Users;
import com.warehouse_management.system.model.*;
import com.warehouse_management.system.service.ISystemService;
import com.warehouse_management.config.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @PACKAGE_NAME: com.warehouse_management.system.controller
 * @NAME: SystemController
 * @USER: WanWenCheng
 * @DATE: 2022/9/30
 * @TIME: 18:51
 * @Description: system控制层
 */
@Controller
@RequestMapping("/system")
public class SystemController {

    @Autowired
    private ISystemService systemService;

    public ISystemService getSystemService() {
        return systemService;
    }

    public void setSystemService(ISystemService systemService) {
        this.systemService = systemService;
    }

//    @RequestMapping("/toPositionGrant")
//    public String toPositionGrant(){
//        return "sys/position/positionGrant";
//    }

    /*
     *
     * @PACKAGE_NAME: com.warehouse_management.system.controller
     * @NAME: toPositionList
     * @USER: WanWenCheng
     * @DATE: 2022/10/1
     * @TIME: 22：35
     * @Description: 增加职位
     * */
    @RequestMapping("/toPositionAdd")
    @ResponseBody
    public String toPositionAdd(Job job){
        JobCondition jc=new JobCondition();
        jc.setJob_name(job.getJob_name());
        jc.setJob_dept_id(Integer.parseInt(job.getJob_dept_id()));
        int i = systemService.addJobPosition(jc);
        if (i>0){
            return JSON.toJSONString(true);
        }else {
            return JSON.toJSONString(false);
        }
    }


    /*
     *
     * @PACKAGE_NAME: com.warehouse_management.system.controller
     * @NAME: toPositionList
     * @USER: WanWenCheng
     * @DATE: 2022/10/1
     * @TIME: 10：15
     * @Description: 职位查询
     * */
    @RequestMapping("/toPositionList")
    @ResponseBody
    public String toPositionList(Job job, PageModel pageModel){
        PageModel jobs = systemService.PageJobPosition(job, pageModel);
        if (jobs.getData()!=null){
            if (!jobs.getData().isEmpty()){
                JSON.toJSONString(jobs);
                return JSON.toJSONString(jobs);
            }
        }
        return null;
    }

    /*
     *
     * @PACKAGE_NAME: com.warehouse_management.system.controller
     * @NAME: toPositionList
     * @USER: WanWenCheng
     * @DATE: 2022/10/2
     * @TIME: 14：50
     * @Description: 搜索查询
     * */
    @RequestMapping("/queryOneJob")
    @ResponseBody
    public String queryOneJob(Job job){
        List jobs = systemService.queryAllJobById(job);
        if (jobs!=null){
            if (!jobs.isEmpty()){
                JSON.toJSONString(jobs);
                return JSON.toJSONString(jobs);
            }
        }
        return null;
    }


    /*
     *
     * @PACKAGE_NAME: com.warehouse_management.system.controller
     * @NAME: toPositionList
     * @USER: WanWenCheng
     * @DATE: 2022/10/1
     * @TIME: 23：35
     * @Description: 修改职位
     * */
    @RequestMapping("/updatePositionView/{job_id}")
    public String updatePositionView(@PathVariable("job_id") String job_id, HttpSession session){
        Job job=new Job();
        job.setJob_id(job_id);
        List<Job> jobList = systemService.queryAllJobById(job);
        if (jobList!=null){
            if (!jobList.isEmpty()){
                session.setAttribute("job",jobList.get(0));
            }
        }
        return "sys/position/positionUpdate";
    }

    @RequestMapping("/toPositionUpdate")
    @ResponseBody
    public String toPositionUpdate(Job job,HttpSession session){
        Job job1 = (Job) session.getAttribute("job");
        JobCondition jc=new JobCondition();
        jc.setJob_name(job.getJob_name());
        jc.setJob_dept_id(Integer.parseInt(job.getJob_dept_id()));
        jc.setJob_id(Integer.parseInt(job1.getJob_id()));
        int i = systemService.updateJobPosition(jc);
        if (i>0){
            return JSON.toJSONString("true");
        }else {
            return JSON.toJSONString("false");
        }

    }

    /*
     *
     * @PACKAGE_NAME: com.warehouse_management.system.controller
     * @NAME: toPositionList
     * @USER: WanWenCheng
     * @DATE: 2022/10/2
     * @TIME: 10：58
     * @Description: 修改职位状态
     * */

    @RequestMapping("/updateJobPositionByState/{job_id}/{job_state}")
    @ResponseBody
    public String updateJobPositionByState(Job job,@PathVariable("job_id") String job_id,@PathVariable("job_state") String job_state){
        JobCondition jc=new JobCondition();
        jc.setJob_state(job_state);
        jc.setJob_id(Integer.parseInt(job_id));
        int i = systemService.updateJobPositionByState(jc);
        if (i>0){
            return JSON.toJSONString("true");
        }else {
            return JSON.toJSONString("false");
        }
    }

    /*
     *
     * @PACKAGE_NAME: com.warehouse_management.system.controller
     * @NAME: toPositionList
     * @USER: WanWenCheng
     * @DATE: 2022/10/1
     * @TIME: 23：35
     * @Description: 赋权
     * */
    @RequestMapping("/fuquanID/{job_id}")
    public String fuquanID(@PathVariable("job_id") String job_id, HttpSession session){
        Job job=new Job();
        job.setJob_id(job_id);
        List<Job> jobList = systemService.queryAllJobById(job);
        if (jobList!=null){
            if (!jobList.isEmpty()){
                session.setAttribute("fuquanID",jobList.get(0));
            }
        }
        return "sys/position/positionGrant";
    }

    //positionGrant页面刷新需要将模块数据发送到前端
    @RequestMapping("/fuzMoKuai0")
    @ResponseBody
    public String fuzMoKuai0(){
        List<Module> modules = systemService.queryAllMoud0();
        if (modules!=null){
            return JSON.toJSONString(modules);
        }
        return JSON.toJSONString("null");
    }
    @RequestMapping("/fuzMoKuai1")
    @ResponseBody
    public String fuzMoKuai1(){
        List<Module> modules = systemService.queryAllMoud1();
        if (modules!=null){
            return JSON.toJSONString(modules);
        }
        return JSON.toJSONString("null");
    }
    @RequestMapping("/fuzMoKuai2")
    @ResponseBody
    public String fuzMoKuai2(){
        List<Module> modules = systemService.queryAllMoud2();
        if (modules!=null){
            return JSON.toJSONString(modules);
        }
        return JSON.toJSONString("null");
    }
    @RequestMapping("/fuzMoKuai3")
    @ResponseBody
    public String fuzMoKuai3(){
        List<Module> modules = systemService.queryAllMoud3();
        if (modules!=null){
            return JSON.toJSONString(modules);
        }
        return JSON.toJSONString("null");
    }
    @RequestMapping("/fuzMoKuai4")
    @ResponseBody
    public String fuzMoKuai4(){
        List<Module> modules = systemService.queryAllMoud4();
        if (modules!=null){
            return JSON.toJSONString(modules);
        }
        return JSON.toJSONString("null");
    }
    @RequestMapping("/fuzMoKuai5")
    @ResponseBody
    public String fuzMoKuai5(){
        List<Module> modules = systemService.queryAllMoud5();
        if (modules!=null){
            return JSON.toJSONString(modules);
        }
        return JSON.toJSONString("null");
    }

    //positionGrant提交赋权之后需要修改数据库中的permission的值
    @RequestMapping("/fuquan")
    @ResponseBody
    public String fuquan(@RequestBody() List<String> str,HttpSession session){
        Permission permission=new Permission();
        Job job = (Job) session.getAttribute("fuquanID");
        String job_id = job.getJob_id();
        String job_state = job.getJob_state();
        permission.setComrid(job_id);
        permission.setComstate(job_state);
        int count=0;
        for (int i=0;i<str.size();i++){
            String moduleId = str.get(i);
            permission.setCompid(moduleId);
            count += systemService.addpermiss(permission);
        }
        if(count==str.size()){
            return JSON.toJSONString("true");
        }
        return JSON.toJSONString("false");

    }


    /**
     * 陈博
     * 根据条件查询所有用户集合
     * @param model
     * @param dept
     * @param pageModel
     * @return
     */
    @RequestMapping("/deptList")
    public String deptList(Model model, Dept dept, PageModel<Users> pageModel){
        PageModel<Users> userPage = systemService.getUserListByUsers(dept, pageModel);
        model.addAttribute("userPage",userPage);
        return "forward:/system/deptView";
    }

    /**
     * 陈博
     * @param dept
     * @param model
     * @return
     */
    @RequestMapping("/addView")
    public String addView(Dept dept,Model model){

        List<Users> roleList = systemService.getUserListByUsers(dept);
        model.addAttribute("roleList",roleList);
        return "useradd";
    }

    /**
     * 林涛
     * @param pageModel
     * @return
     * 分页查询用户
     */
    @RequestMapping("/getUserList")
    @ResponseBody
    public String getUserList(PageModel<Users> pageModel){
        PageModel<Users> userList = systemService.getUserList(pageModel);
        if (userList.getData()!=null){
            if (!userList.getData().isEmpty()){
                JSON.toJSONString(userList);
                return JSON.toJSONString(userList);
            }
        }
        return null;
    }


    /**
     * 连小龙
     * @param module
     * @param pageModel
     * @return
     * 查询系统管理（模块管理页面）首页数据
     */
    @RequestMapping("/getPageModuleList")
    @ResponseBody
    public String getPageModuleList(Module module, PageModel<ModuleCondition> pageModel){
        PageModel<ModuleCondition> models = systemService.PageModulePosition(module, pageModel);
        if (models.getData()!=null){
            if (!models.getData().isEmpty()){
                JSON.toJSONString(models);
                return JSON.toJSONString(models);
            }
        }
        return null;
    }

    /**
     * 连小龙
     * 模块搜索查询
     * @param
     * @return
     */
    @RequestMapping("/queryOneModule")
    @ResponseBody
    public String queryOneModule(@RequestParam("module_rname") String module_rname, PageModel<ModuleCondition> pageModel){
        Module module = new Module();
        module.setModule_rname(module_rname);
        PageModel<ModuleCondition> moduleList = systemService.queryAllModuleByName(module,pageModel);
        if (moduleList.getData()!=null){
            if (!moduleList.getData().isEmpty()){
                JSON.toJSONString(moduleList);
                return JSON.toJSONString(moduleList);
            }
        }
        return null;
    }

    /**
     * 连小龙
     * @param module_id
     * @param
     * @return
     * 修改模块
     */
    @RequestMapping("/updateModuleView/{module_id}")
    public String updateModuleView(@PathVariable("module_id") String module_id,Model model){
        List<Module> moduleList = systemService.queryAllModuleById(module_id);
        for (Module module : moduleList) {
            model.addAttribute("module",module);
            System.out.print(model);
        }
        return "sys/modules/moduleUpdate";
    }

    /**
     * 连小龙
     * @param module_id
     * @param
     * @return
     * 修改选中的模块数据
     */
    @RequestMapping("/updateModule")
    @ResponseBody
    public String updateModule(@RequestParam("module_id") String module_id,@RequestParam("module_rname") String module_rname,@RequestParam("module_pid") String module_pid,@RequestParam("module_murl") String module_murl){
        Module module = new Module();
        module.setModule_rname(module_rname);
        module.setModule_id(module_id);
        module.setModule_pid(module_pid);
        module.setModule_murl(module_murl);
        int i = systemService.updateModulePosition(module);
        if (i>0){
            return JSON.toJSONString("true");
        }else {
            return JSON.toJSONString("false");
        }
    }

    /**
     * 连小龙
     * @param
     * @param
     * @return
     * 增加模块数据
     */
    @RequestMapping("/addModule")
    @ResponseBody
    public String addModule(@RequestParam("module_rname") String module_rname,@RequestParam("module_pid") String module_pid,@RequestParam("module_murl") String module_murl,@RequestParam("module_state") String module_state){
        Module module = new Module();
        module.setModule_rname(module_rname);
        module.setModule_pid(module_pid);
        module.setModule_murl(module_murl);
        module.setModule_state(module_state);
        module.setModule_id("31");
        int i = systemService.addModulePosition(module);
        if (i>0){
            return JSON.toJSONString("true");
        }else {
            return JSON.toJSONString("false");
        }
    }


    /**
     * 连小龙
     * 点击（禁用或启动）功能
     * @param
     * @param module_id
     * @param module_state
     * @return
     */
    @RequestMapping("/updateModulePositionByState/{module_id}/{module_state}")
    @ResponseBody
    public String updateModulePositionByState(@PathVariable("module_id") String module_id,@PathVariable("module_state") String module_state){
        Module md = new Module();
        md.setModule_state(module_state);
        md.setModule_id(module_id);
        int i = systemService.updateModulePositionByState(md);
        if (i>0){
            return JSON.toJSONString("true");
        }else {
            return JSON.toJSONString("false");
        }
    }

    /**
     * 周永成
     * 查询日志
     * @return 日志集合
     */
    @RequestMapping("/getJournalList/{total}")
    @ResponseBody
    public PageModel<Journal> queryAllJournal(PageModel<Journal> pageModel,
                                              @PathVariable Integer total){
        pageModel.setPageNum(total);
        return systemService.queryAllJournal(pageModel);
    }

    /**
     * 周永成
     * 查询用户日志
     * @param id
     * @return
     */
    @RequestMapping("/getJournalById/{id}")
    public String queryOneJournal(@PathVariable Integer id,Model model){
        Journal journal = systemService.queryJournalById(id);
        model.addAttribute("journal",journal);
        return "/sys/logs/logView.html";
    }

    /**
     *
     * 周永成
     * 模糊查询
     * @param j_Id              员工编号
     * @param uname             员工姓名
     * @param jcontent          日志内容
     * @param module_rname      操作模块
     * @param jdate1            记录时间1
     * @param jdate2            记录时间2
     * @param pageModel         分页模型
     * @param total             当前页数
     * @return
     */
    @RequestMapping("/queryJournalByLike/{j_Id}/{uname}/{jcontent}/{module_rname}/{jdate1}/{jdate2}/{total}")
    @ResponseBody
    public PageModel<Journal> queryJournalByLike(@PathVariable("j_Id") Integer j_Id,
                                            @PathVariable("uname") String uname,
                                            @PathVariable("jcontent") String jcontent,
                                            @PathVariable("module_rname") String module_rname,
                                            @PathVariable("jdate1") String jdate1,
                                            @PathVariable("jdate2") String jdate2,
                                            PageModel<Journal> pageModel,
                                            @PathVariable Integer total){

        pageModel.setPageNum(total);

        return systemService.queryJournalByLike(j_Id,uname,jcontent,module_rname,jdate1,jdate2,pageModel);

    }

    /**
     * 邹佳乐
     * @return
     */
    @RequestMapping("/getareaList/{sheng}/{shi}")
    @ResponseBody
    public List<City> getareaList(@PathVariable String sheng ,@PathVariable String  shi) {

        sheng = sheng == null ? "0" : sheng;
        shi = shi == null ? "0" : shi;

        List<City> cities = systemService.queryAllCity(sheng, shi);
        return cities;
    }
}
